package cn.com.jslh.boarpro.main.presenter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimAdapterEx;
import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.main.contract.SearchResultContract;
import cn.com.jslh.boarpro.main.view.SearchItemInfoActivity;
import cn.com.jslh.boarpro.util.JSONUtil;
import cn.com.jslh.boarpro.util.StartActivityUtil;

/**
 * Created by Administrator on 2017/11/13.
 */

public class SearchResultPresenterImpl implements SearchResultContract.Presenter {
    private SearchResultContract.View view;
    private Context context;
    private SlimAdapterEx adapter;
    private JSONArray headerJsonArray;
    private JSONArray dataJsonArray;
    private int[] dWidth;

    public SearchResultPresenterImpl(Context context, SearchResultContract.View view) {
        view.setPresenter(this);
        this.view = view;
        this.context = context;
    }

    @Override
    public void start() {
        if (dataJsonArray != null)
            adapter.updateData(JSONUtil.JsonArray2List(dataJsonArray)).notifyDataSetChanged();
    }


    @Override
    public void setData(String s) {
        JSONArray jsonArray = null;
        dataJsonArray = new JSONArray();
        try {
            jsonArray = new JSONArray(s);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                if (jsonObject.getString("isdefault").equals("0")) {
                    headerJsonArray = jsonObject.getJSONArray("resultarray");
                } else {
                    dataJsonArray.put(jsonObject);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setHeader(LinearLayout linearLayout) {
        WindowManager manager = ((Activity) context).getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        final int width = outMetrics.widthPixels;
        dWidth = new int[headerJsonArray.length()];
        linearLayout.setBackgroundColor(context.getResources().getColor(R.color.titleBgColor));
        try {
            for (int i1 = 0; i1 < headerJsonArray.length(); i1++) {
                TextView textView = new TextView(context);
                String valueStr = headerJsonArray.getJSONObject(i1).getString("data");
                String[] values = valueStr.split(",");
                textView.setTextColor(Color.WHITE);
                textView.setTextSize(14);
                if(valueStr.equals("0")){
                    textView.setText(context.getResources().getString(R.string.idStr));
                }else {
                    textView.setText(values[0]);
                }
                textView.setGravity(Gravity.CENTER);
                if(values.length<2){
                    dWidth[i1]=100;
                }else {
                    dWidth[i1] = (int) (Double.parseDouble(values[1]) * (width-100));
                }
                textView.setWidth(dWidth[i1]);
                linearLayout.addView(textView);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void bindRV(RecyclerView recyclerView) {
        adapter = SlimAdapter.create(SlimAdapterEx.class)
                .enableDiff()
                .enableEmptyView(context, R.layout.empty_layout)
                .registerDefault(R.layout.search_result_item_layout, new SlimInjector() {
                    @Override
                    public void onInject(Object data, IViewInjector injector) {
                        final JSONObject jsonObject = (JSONObject) data;
                        injector
                                .with(R.id.layout, new IViewInjector.Action() {
                            @Override
                            public void action(View view) {
                                LinearLayout linearLayout = (LinearLayout) view;
                                try {
                                    if (jsonObject.getString("isdefault").equals("1")) {
                                        JSONArray resultarray = jsonObject.getJSONArray("resultarray");
                                        for (int i1 = 0; i1 < resultarray.length(); i1++) {
                                            String valueStr = resultarray.getJSONObject(i1).getString("data");
                                            if (linearLayout.getChildCount() < resultarray.length()) {
                                                TextView textView = new TextView(context);
                                                textView.setText(valueStr);
                                                textView.setTextSize(14);
                                                textView.setGravity(Gravity.CENTER);
                                                textView.setWidth(dWidth[i1]);
                                                linearLayout.addView(textView);
                                            } else {
                                                TextView textView = (TextView) linearLayout.getChildAt(i1);
                                                textView.setText(valueStr);
                                            }
                                        }
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        })
                        .clicked(R.id.layout, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                try {
                                    String fid = jsonObject.getString("fid");
                                    new StartActivityUtil.Builder(context, SearchItemInfoActivity.class)
                                           .addValue("fid",fid)
                                            .build();
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        })
                        ;
                    }
                })
                .attachTo(recyclerView);
    }
}
